package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName:ShopController
 * @Author:泛微-boliu14
 * @Date:2023/11/10 15:02
 * @Description:店铺管理-主要涉及店铺的营业状态
 */

@RestController(value = "adminShopController")
@RequestMapping("/admin/shop")
@Api(tags = "店铺相关接口")
@Slf4j
public class ShopController {


    private static final String key = "SHOP_STATUS";
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 设置店铺的营业状态
     * @param status
     * @return
     */
    @PutMapping("/{status}")
    @ApiOperation(value = "设置店铺营业状态")
    public Result<Integer> status(@PathVariable("status") Integer status){
        log.info("设置店铺的状态为:{}",status==StatusConstant.DISABLE?"打烊中":"营业中");
        //修改店铺状态
        redisTemplate.opsForValue().set(key,status);
        return Result.success();
    }


    /**
     * 管理端获取店铺营业状态
     * @return
     */
    @GetMapping("/status")
    @ApiOperation(value = "获取店铺营业状态")
    public Result status(){
        log.info("管理端获取店铺的状态...");
        //判断redis中是否存在店铺状态
        Boolean hasKey = redisTemplate.hasKey(key);
        if (!hasKey){
            //如果不存在设置默认值
            redisTemplate.opsForValue().set(key, StatusConstant.DISABLE);
        }
        Integer status = (Integer) redisTemplate.opsForValue().get(key);
        log.info("管理端获取店铺的状态为:{}",status==StatusConstant.DISABLE?"打烊中":"营业中");
        return Result.success(status);
    }
}
